package com.yjjxt

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Hello17Repartition {
  def main(args: Array[String]): Unit = {
    val sparkContext = new SparkContext((new SparkConf().setMaster("local").setAppName("Join" + System.currentTimeMillis())))

    val array = Array[String]("user1", "user2", "user3", "user4", "user5", "user6", "user7", "user8")
    val lines = sparkContext.parallelize(array, 4)

    val linesRP: RDD[String] = lines.repartition(2)
    println(lines.getNumPartitions + "--" + linesRP.getNumPartitions)

    lines.mapPartitionsWithIndex((idx, iter) => {
      //遍历一下
      while (iter.hasNext) {
        println(idx + "--" + iter.next())
      }
      iter
    }).count()

    linesRP.mapPartitionsWithIndex((idx, iter) => {
      //遍历一下
      while (iter.hasNext) {
        println(idx + "--" + iter.next())
      }
      iter
    }).count()

    sparkContext.stop()

  }
}
